package com.kehongborui.ticket.controller;

import com.kehongborui.ticket.model.UserModel;
import com.kehongborui.ticket.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-07
 */
@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @GetMapping("/login")// @RequestMapping(method = RequestMapping.GET)
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String login(UserModel model, HttpServletRequest req) {
        // 调用登录业务逻辑
        UserModel userModel = loginService.login(model);
        if (userModel == null) {
            req.setAttribute("msg", "用户名不存在");
            req.setAttribute("username", model.getUsername());
            return "login";
        }
        // 登录成功，把登录用户信息，放到session中
        req.getSession().setAttribute("LOGIN_USER", userModel);
        return "redirect:success";
    }

    /**
     * 返回登录成功后的主页面
     * @return
     */
    @GetMapping("/success")
    public String portal() {
        return "success";
    }
}
